package dom.parser;

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

import dom.pojo.Book;

public class ParserBooks {
	public static void main(String[] args) throws Exception {
		Document doc = new SAXBuilder(false).build(new FileInputStream("books.xml"));
		Element root = doc.getRootElement();
		System.out.println(root.getName());
		
		List<Element> bookstore = root.getChildren();
		List<Book> set = new ArrayList<Book>();
		String type,ISbN,title,author,price;
		Book book;
		for(int i=0; i<bookstore.size(); i++){
			 type = bookstore.get(i).getAttributeValue("Type");
			 ISbN = bookstore.get(i).getAttributeValue("ISBN");
			 title = bookstore.get(i).getChild("title").getTextTrim();
			 author = bookstore.get(i).getChild("author").getTextTrim();
			 price = bookstore.get(i).getChild("price").getTextTrim();
			 book = new Book(type, ISbN, title, author, price);
			System.out.println(book.toString());
			set.add(book);
		}
		System.out.println("Totally "+set.size()+" books!");
		
	}

}
